/// 某个医院的租金汇总列表

var util = require('../../../../utils/util.js');
var app = getApp()
//常量数据配置
var ConstantData = require('../../../../common/constant_data.js')
//获取应用实例  
Page({

  data: {

    tabList: [],
    leaseData: '',
    hospitalName: '',

    currentTab: 11, //默认是最后一个TAB
    payMinYear: 0,
    payMinMonth: 0
  },



  onLoad: function (options) {
    var that = this;

    var mecId = options.mecId;
    var hospitalName = options.hospitalName;
    var isNoReal = options.isNoReal;
    var startDate = util.getCurMonth() + "-01";
    var endDate = util.getNextMonth() + "-01";
    this.data.isNoReal = isNoReal;
    this.setData({
      mecId: mecId,
      hospitalName: hospitalName
    });


    that.init(startDate, endDate);


    console.log(that.data.hospitalList);

  },
  /**TAB 页切换
   * 进行数据切换
   */
  swichNav: function (e) {
    var that = this;
    console.log(e);
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      });

      var curMonth = e.target.dataset.month;
      var curYear = e.target.dataset.year;
      var startDate = curYear + "-" + (curMonth < 10 ? "0" + curMonth : curMonth) + "-01";

      var nextMonth = curMonth + 1;
      var nextYear = curYear;
      if (curMonth == 12) {
        nextMonth = 1;
        nextYear = curYear + 1;
      }
      var endDate = nextYear + "-" + (nextMonth < 10 ? "0" + nextMonth : nextMonth) + "-01";
      that.init(startDate, endDate);
    }
  },
  /** 
   * 点击
   */
  toDetail: function (e) {

    var that = this;
    console.log(e);
    var date = e.target.dataset.date;
    wx.navigateTo({
      url: '../rentaldetail/rentaldetail?mecId=' + that.data.mecId + "&hospitalName=" + that.data.hospitalName + "&date=" + date + "&startDate=" + that.data.startDate +
        "&isNoReal=" + that.data.isNoReal + '&state=0',
    });
    var id = e.currentTarget.dataset.hospitalid;
    console.log("hospitalid:" + id + "---");
  },

  /** 
   * 点击
   */
  toRebackDetail: function (e) {

    var that = this;
    console.log(e);
    var date = e.target.dataset.date;
    wx.navigateTo({
      url: '../rentaldetail/rentaldetail?mecId=' + that.data.mecId + "&hospitalName=" + that.data.hospitalName + "&date=" + date + "&startDate=" + that.data.startDate +
        "&isNoReal=" + that.data.isNoReal + '&state=1',
    });
    var id = e.currentTarget.dataset.hospitalid;
    console.log("hospitalid:" + id + "---");
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // wx.showLoading({
    //   title: 'loading',
    // });
    // setTimeout(function(){
    // getApp().hideLoading();
    // },3000);
    var that = this;
    if (that.data.loading && !that.data.loadingComplete) {
      that.setData({
        pageNum: that.data.pageNum + 1, //每次触发上拉事件，把searchPageNum+1  
      });
      that.getApageLeaseRecord();
    }
  },


  init: function (startDate, endDate) {
    var list = getMonthList();

    var that = this;
    this.setData({
      startDate: startDate,
      endDate: endDate
    })
    app.executeRequest(function (header) {
      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_statistic/lease_pay/listData',
        header: header,
        data: {
          startDate: startDate,
          endDate: endDate,
          mecId: that.data.mecId,
          isNoReal: that.data.isNoReal
        },
        success: function (res) {
          console.log("pppp---->", res);
          var incomeList = Array();
          var totalData = {
            "payDate": '总',
            "income": "0",
            //总单数
            "counts": "0",
            "isShowCounts": false,
            "detailBtnIsViewFlg": true
          };
          let rstData = res.data
          incomeList.push(totalData);
          var totalIncome = 0;
          if(rstData.viewPayMinDate) {
            //最低的查看时间
            let minDateArr = rstData.viewPayMinDate.split('-')
            that.setData({
              payMinYear: parseInt(minDateArr[0]),
              payMinMonth: parseInt(minDateArr[1])
            })
          }
          if (rstData.totalRentVal) {
            totalIncome = rstData.totalRentVal
            incomeList[0].income = totalIncome;
            incomeList[0].isShowCounts = rstData.showCountsFlg;
            incomeList[0].counts = rstData.counts;
            incomeList[0].detailBtnIsViewFlg = rstData.detailBtnIsViewFlg;
          } else {
            if(rstData.data) {
              for (var i = 0; i < rstData.data.length; i++) {
                totalIncome += rstData.data[i].income;
                incomeList.push(rstData.data[i]);
              }
            }            
            incomeList[0].income = parseFloat(totalIncome.toFixed(2));
            incomeList[0].detailBtnIsViewFlg = rstData.data.length > 0 ? rstData.data[0].detailBtnIsViewFlg : true;
          }
          that.setData({
            tabList: list,
            leaseData: incomeList
          })
        }
      })
    });

  }

})



//得到月份的列表，目前只默认查看最近6个月的数据
function getMonthList() {
  var date = new Date();
  var year = date.getFullYear(); //得到当前年份
  var month = date.getMonth() + 1; //得到当前月份，getMonth（）从0-11 ，所以此处需加1


  var list = new Array();
  if (month >= 6) {

    for (var i = 11; i >= 0; i--) {
      var obj = new Object();
      obj.year = year;
      obj.month = month - i;
      if (obj.month <= 0) {

        obj.year = year - 1;
        obj.month = (obj.month + 12);
        obj.name = obj.month;
      } else {
        obj.name = obj.month;
      }

      list.push(obj);
    }
  } else {
    for (var i = 11; i >= 0; i--) {
      var obj = new Object();
      obj.year = year;
      obj.month = month - i;
      if (obj.month <= 0) {

        obj.year = year - 1;
        obj.month = (obj.month + 12);
        // obj.name = obj.year + "." + obj.month;
        obj.name = obj.month;
      } else {
        obj.name = obj.month;
      }
      list.push(obj);
    }

  }

  console.log(list);
  return list;

}

function test() {

  var date = new Date();
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();


  console.log("year=" + year);
  console.log("month=" + month);
  console.log("day=" + day);
}